package com.moguhoutai.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.moguhoutai.biz.AdminBiz;
import com.moguhoutai.model.Admin;
import com.moguhoutai.util.BusinessException;
import com.moguhoutai.util.Constants;

@Controller
@RequestMapping("/admin")
public class AdminController {

	@Resource
	private AdminBiz adminBiz;

	@RequestMapping("/login/page")
	public String toLogin() {
		return "/login";
	}
	
	@RequestMapping("/welcome")
	public String welcome() {
		return "/welcome";
	}
	
	@RequestMapping("/login")
	public String login(Admin user, HttpSession session) throws BusinessException {

		try {
			Admin admin = adminBiz.login(user);
			if (admin == null) {
				throw new BusinessException(Constants.LOGIN_EXCEPTION_CODE);
			} else {
				session.setAttribute("currentUser", admin);
				return "/index";
			}
		} catch (BusinessException e) {
			e.printStackTrace();
			throw e;
		}
	}
}
